﻿// TODO Localizzazione dei messagebox
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CompactFramework.Utilities.Localization;

namespace TripLOAMobile
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MapSelection : LocalizedForm
    {
        private AnimationControl animation;

        string latitude, longitude, address;
        bool addressAcquired = false;
        /// <summary>
        /// Initializes a new instance of the MapSelection class.
        /// </summary>
        public MapSelection()
        {
            InitializeComponent();
            #region creation of the control for the animation
            animation = new AnimationControl();
            int x = this.Width / 2 - animation.Width / 2;
            int y = this.Height / 2 - animation.Height / 2;
            animation.Location = new Point(x, y);
            this.Controls.Add(animation);
            animation.BringToFront();
            #endregion
            this.addressBox.FillPanel = this.addressPanel;
            
            this.AddMenusToDictionary();
            UpdateResources();
            this.gmmControl1.LoadMap();
        }

        /// <summary>
        /// Initializes a new instance of the MapSelection class.
        /// </summary>
        /// <param name="lat">The latitude used to center the map.</param>
        /// <param name="lon">The longitude used to center the map.</param>
        public MapSelection(double lat, double lon)
        {
            InitializeComponent();
            #region creation of the control for the animation
            animation = new AnimationControl();
            int x = this.Width / 2 - animation.Width / 2;
            int y = this.Height / 2 - animation.Height / 2;
            animation.Location = new Point(x, y);
            this.Controls.Add(animation);
            animation.BringToFront();
            #endregion
            this.AddMenusToDictionary();
            UpdateResources();
            this.gmmControl1.LoadMap(lat, lon);
        }

        /// <summary>
        /// Gets the latitude.
        /// </summary>
        /// <value>The latitude.</value>
        public double Latitude
        {
            get
            {
                return Convert.ToDouble(latitude);
            }
        }

        /// <summary>
        /// Gets the longitude.
        /// </summary>
        /// <value>The longitude.</value>
        public double Longitude
        {
            get
            {
                return Convert.ToDouble(longitude);
            }
        }

        /// <summary>
        /// Gets a value indicating whether an address is acquired.
        /// </summary>
        /// <value><c>true</c> if an address is acquired; otherwise, <c>false</c>.</value>
        public bool AddressAcquired
        {
            get
            {
                return addressAcquired;
            }
        }

        /// <summary>
        /// The subclasses of the LocalizedForm should override this method and use this method to
        /// associate all menu items with custom names with the help of AddMenuToDictionary
        /// method.
        /// </summary>
        protected override void AddMenusToDictionary()
        {
            this.AddMenuToDictionary(this.menuLeft, "menuLeft");
            this.AddMenuToDictionary(this.menuRight, "menuRight");
            this.AddMenuToDictionary(this.menuItem1, "menuBack");
            this.AddMenuToDictionary(this.menuItem2, "menuHome");
        }

        /// <summary>
        /// Handles the SelectedLocationChanged event of the gmmControl1 control.
        /// Used to save the selected latitude and longitude selected by the map.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        void gmmControl1_SelectedLocationChanged(object sender, System.EventArgs e)
        {
            GMMGetTiles.GeoCoordinate coords = gmmControl1.SelectedLocation;
            latitude = coords.lat.ToString();
            longitude = coords.lon.ToString();
        }

        /// <summary>
        /// Gets the selected location.
        /// </summary>
        /// <value>The selected location.</value>
        public string SelectedLocation
        {
            get { return ((latitude != null) && (longitude != null ) && !latitude.Equals("") && !longitude.Equals("")) ? string.Format("{0},{1}", latitude, longitude) : ""; }
        }

        /// <summary>
        /// Gets the selected address.
        /// </summary>
        /// <value>The selected address.</value>
        public string SelectedAddress
        {
            get { return (address != null) ?address :"";}
        }

        #region OnPainting
        /// <summary>
        /// Handles the Paint event.
        /// </summary>
        /// <param name="e">A PaintEventArgs that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            using (Graphics g = e.Graphics)
            {
                Point[] p = new Point[]{
                    new Point(40,25), 
                    new Point(195,25), 
                    new Point(190,30), 
                    new Point(40,30)};
                using (Brush b = new SolidBrush(Color.Green))
                    g.FillPolygon(b, p);
                p[0].Y = p[1].Y = 30;
                p[2].Y = p[3].Y = 35;
                p[1].X -= 10;
                p[2].X -= 10;

                using (Brush b = new SolidBrush(Color.GreenYellow)) g.FillPolygon(b, p);

                g.DrawImage(Properties.Resources.circles, 8, 5);

                int left = this.lblAddress.Left - 6;
                int right = this.btnFind.Right + 6;
                int top = this.lblAddress.Top - 6;
                int bottom = this.txtAddress.Bottom + 6;
                int roundDiameter = 10;
                Rectangle r = new Rectangle(left, top, roundDiameter, roundDiameter);
                using (Brush b = new SolidBrush(Color.FromArgb(222, 241, 126)))
                {                  
                    g.FillEllipse(b, r);
                    r.X = right - roundDiameter - 1;
                    g.FillEllipse(b, r);
                    r.Y = bottom - roundDiameter - 1;
                    g.FillEllipse(b, r);
                    r.X = left;
                    g.FillEllipse(b, r);
                    r.X = left + roundDiameter / 2; r.Y = top; r.Width = right - left - roundDiameter; r.Height = bottom - top;
                    g.FillRectangle(b, r);
                    r.X = left; r.Y = top + roundDiameter / 2; r.Width = roundDiameter / 2; r.Height = bottom - top - roundDiameter;
                    g.FillRectangle(b, r);
                    r.X = right - roundDiameter / 2;
                    g.FillRectangle(b, r);
                }

                using (Brush b = new SolidBrush(Color.FromArgb(48, 138, 13)))
                {
                    r.X = txtAddress.Location.X - 3; r.Y = txtAddress.Location.Y - 3; r.Width = txtAddress.Width + 6; r.Height = txtAddress.Height + 6;
                    g.FillRectangle(b, r);
                    r.X = btnFind.Location.X - 3; r.Y = btnFind.Location.Y - 3; r.Width = btnFind.Width + 6; r.Height = btnFind.Height + 6;
                    g.FillRectangle(b, r);
                    r.X = gmmControl1.Location.X - 3; r.Y = gmmControl1.Location.Y - 3; r.Width = gmmControl1.Width + 6; r.Height = gmmControl1.Height + 6;
                    g.FillRectangle(b, r);
                }

            }

            //Added to prevent painting of "ok" button
            WinAPI.ShowDoneButton(this.Handle, false);

            base.OnPaint(e);
        }
        #endregion;

        /// <summary>
        /// Handles the Click event of the btnFind control.
        /// Used to search an address and shows it on the map control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs" instance containing the event data.</param>
        private void btnFind_Click(object sender, EventArgs e)
        {
            Welcome.rrp.CheckAddress(this, rrp_GeocodeResponse, addressBox.FullText);
        }

        #region ResponseHandling
        /// <summary>
        /// Handles the geocode response.
        /// </summary>
        /// <param name="e">The <see cref="TripLOAMobile.RemoteResourceProvider.GeoCodeResponseEventArgs"/> instance containing the event data.</param>
        private void rrp_GeocodeResponse(RemoteResourceProvider.GeoCodeResponseEventArgs e)
        {
            if (e.Status == RemoteResourceProvider.okStatus)
            {
                string[] tmp = e.Address.Split(',');
                latitude = tmp[0];
                longitude = tmp[1];
                try
                {
                    this.gmmControl1.LoadMap(Convert.ToDouble(tmp[0]), Convert.ToDouble(tmp[1]));
                }
                catch (FormatException)
                {
                    MessageBox.Show(ResourceManager.Instance.GetString("InternalError"), "Format Exception", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2);
                    txtAddress.Text = "";
                    return;
                }
            }
            else
            {
                txtAddress.Text = "";
                MessageBox.Show(e.Status);
            }
        }
        #endregion

        /// <summary>
        /// Handles the Click event of the menuRight(accept) control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuRight_Click(object sender, EventArgs e)
        {
            addressAcquired = true;
            Welcome.formBuffer.Close();
        }

        /// <summary>
        /// Handles the Click event of the menuItem1(back) control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuItem1_Click(object sender, EventArgs e)
        {
            addressAcquired = false;
            Welcome.formBuffer.Close();
        }

        /// <summary>
        /// Handles the Click event of the menuItem2(back to home) control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The EventArgs instance containing the event data.</param>
        private void menuItem2_Click(object sender, EventArgs e)
        {
            this.gmmControl1.Dispose();
            Welcome.formBuffer.Clear();
        }

        #region start/stop animation
        delegate void animationDel();
        /// <summary>
        /// Stops the animation.
        /// </summary>
        private void stopAnimation()
        {
            foreach (Control c in this.Controls)
            {
                c.Enabled = true;
            }
            this.menuLeft.Enabled = true;
            this.menuRight.Enabled = true;
            this.animation.StopAnimation();
        }
        /// <summary>
        /// Starts the animation.
        /// </summary>
        private void startAnimation()
        {
            foreach (Control c in this.Controls)
            {
                c.Enabled = false;
            }
            this.menuLeft.Enabled = false;
            this.menuRight.Enabled = false;
            this.animation.StartAnimation();
        }
        #endregion
    }
}